"""
仪表盘API测试
"""
from django.test import TestCase
from django.urls import reverse
from rest_framework.test import APITestCase
from rest_framework import status
from django.contrib.auth import get_user_model

from students.models import Student
from resume_management.models import Resume
from interviews.models import Interview
from jobs.models import Job, JobApplication

User = get_user_model()


class DashboardAPITestCase(APITestCase):
    """仪表盘API测试用例"""
    
    def setUp(self):
        """设置测试数据"""
        # 创建测试用户
        self.admin_user = User.objects.create_user(
            username='admin',
            email='admin@test.com',
            password='testpass123',
            role='admin'
        )
        
        self.teacher_user = User.objects.create_user(
            username='teacher',
            email='teacher@test.com',
            password='testpass123',
            role='teacher'
        )
        
        self.student_user = User.objects.create_user(
            username='student',
            email='student@test.com',
            password='testpass123',
            role='student'
        )
        
        # 创建学生记录
        self.student = Student.objects.create(
            user=self.student_user,
            name='测试学生',
            student_id='2024001',
            phone='13800138000',
            email='student@test.com',
            employment_status='unemployed'
        )
    
    def test_overview_stats_admin(self):
        """测试管理员概览统计"""
        self.client.force_authenticate(user=self.admin_user)
        url = reverse('dashboard:overview_stats')
        response = self.client.get(url)
        
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertIn('total_students', response.data)
        self.assertIn('total_resumes', response.data)
        self.assertIn('total_interviews', response.data)
        self.assertIn('total_jobs', response.data)
    
    def test_student_employment_stats(self):
        """测试学生就业统计"""
        self.client.force_authenticate(user=self.admin_user)
        url = reverse('dashboard:student_employment_stats')
        response = self.client.get(url)
        
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertIn('employed', response.data)
        self.assertIn('unemployed', response.data)
        self.assertIn('further_study', response.data)
    
    def test_student_personal_stats(self):
        """测试学生个人统计"""
        self.client.force_authenticate(user=self.student_user)
        url = reverse('dashboard:student_my_statistics')
        response = self.client.get(url)
        
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertIn('resumeCount', response.data)
        self.assertIn('interviewCount', response.data)
        self.assertIn('applicationCount', response.data)
    
    def test_teacher_students_stats(self):
        """测试教师学生统计"""
        self.client.force_authenticate(user=self.teacher_user)
        url = reverse('dashboard:teacher_students_stats')
        response = self.client.get(url)
        
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertIn('total_students', response.data)
        self.assertIn('employment_stats', response.data)
    
    def test_unauthorized_access(self):
        """测试未授权访问"""
        url = reverse('dashboard:overview_stats')
        response = self.client.get(url)
        
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
    
    def test_role_permission(self):
        """测试角色权限"""
        # 学生尝试访问管理员接口
        self.client.force_authenticate(user=self.student_user)
        url = reverse('dashboard:system_overview')
        response = self.client.get(url)
        
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)


class DashboardUtilsTestCase(TestCase):
    """仪表盘工具函数测试"""
    
    def setUp(self):
        """设置测试数据"""
        self.user = User.objects.create_user(
            username='testuser',
            email='test@test.com',
            password='testpass123',
            role='student'
        )
        
        self.student = Student.objects.create(
            user=self.user,
            name='测试学生',
            student_id='2024001',
            phone='13800138000',
            email='test@test.com',
            employment_status='unemployed'
        )
    
    def test_calculate_profile_completion(self):
        """测试档案完成度计算"""
        from dashboard.student_views import calculate_profile_completion
        
        completion = calculate_profile_completion(self.student)
        self.assertIsInstance(completion, float)
        self.assertGreaterEqual(completion, 0)
        self.assertLessEqual(completion, 100)
    
    def test_calculate_employment_progress_score(self):
        """测试就业进展分数计算"""
        from dashboard.teacher_views import calculate_employment_progress_score
        
        score = calculate_employment_progress_score(self.student, 1, 2, 1)
        self.assertIsInstance(score, int)
        self.assertGreaterEqual(score, 0)
        self.assertLessEqual(score, 100)
